﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Gollie.DataTables;

namespace Gollie.Controls
{
	public partial class MyVendorOffers : AuthenticationRequiredSalesBaseUserControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
		}

		protected override void OnFirstHit(object sender, EventArgs e)
		{
			VendorOffers offers = new VendorOffers(this.UserInfo);

			this.lblNoOffersYet.Visible = offers.Count == 0;

			this.GridView1.DataSource = offers;

			this.GridView1.DataBind();

			string vendorDealsUrl = ConfigAppSettings.GetValue("VendorDealsUrl") + "?v=" + this.UserInfo.VendorId.ToString();
			this.hplConsumerLink.NavigateUrl = vendorDealsUrl;
			this.hplConsumerLink.Text = vendorDealsUrl;

			this.lblHtmlSnippet.Text = this.UserInfo.VendorInfo.BedrijfsNaam + " werkt samen met &lt;a href=&quot;https://www.gollie.nl&quot; target=&quot;_blank&quot;&gt;Gollie&lt;/a&gt;. Hierdoor kunnen wij u &lt;a href=&quot;" + vendorDealsUrl + "&quot; target=&quot;_blank&quot;&gt;zeer spectaculaire deals&lt;/a&gt; aanbieden!";

			this.lblHtmlResult.Text = HttpUtility.HtmlDecode(this.lblHtmlSnippet.Text);
		}

		protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			if (e.CommandName != "EditRow" && e.CommandName != "DeleteRow" && e.CommandName != "WithdrawRow")
				return;

			string[] arg = e.CommandArgument.ToString().Split('|');
			int offerId = Convert.ToInt32(arg[0]);
			int rowIndex = Convert.ToInt32(arg[1]);

			switch (e.CommandName)
			{
				case "EditRow":
					this.EditRow(offerId, rowIndex);
					break;
				case "DeleteRow":
					this.DeleteRow(offerId, rowIndex);
					break;
				case "WithdrawRow":
					this.WithdrawRow(offerId, rowIndex);
					break;
			}
		}

		private void EditRow(int offerId, int rowIndex)
		{
			Response.Redirect("/EditOffer.aspx?o=" + offerId.ToString(), true);
		}

		private void DeleteRow(int offerId, int rowIndex)
		{
			long rowsAffected = Database.Queries.CancelOffer((uint)offerId);

			VendorOffers offers = new VendorOffers(this.UserInfo);
			this.lblNoOffersYet.Visible = offers.Count == 0;

			this.GridView1.DataSource = offers;
			this.GridView1.DataBind();

			if (rowsAffected == 0)
			{
				this.ErrorMessageControl1.Text = "Deal kon niet verwijderd worden. Vermoedelijk is deze zojuist verkocht.";
				this.ErrorMessageControl1.Show();
			}
			else
				this.ErrorMessageControl1.Hide();
		}

		private void WithdrawRow(int offerId, int rowIndex)
		{
			long rowsAffected = Database.Queries.WithdrawOffer((uint)offerId);

			VendorOffers offers = new VendorOffers(this.UserInfo);
			this.lblNoOffersYet.Visible = offers.Count == 0;

			this.GridView1.DataSource = offers;
			this.GridView1.DataBind();

			if (rowsAffected == 0)
			{
				this.ErrorMessageControl1.Text = "Deal kon niet teruggetrokken worden. Neem contact op met Gollie.nl.";
				this.ErrorMessageControl1.Show();
				logger.Warn("Terugtrekking mislukt, voor deal {0}", offerId);
			}
			else
				this.ErrorMessageControl1.Hide();
		}

		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				foreach (TableCell cell in e.Row.Cells)
				{
					try
					{
						DateTime validTo = Convert.ToDateTime(e.Row.Cells[6].Text);
						if (validTo < DateTime.Now)
						{
							cell.BackColor = System.Drawing.Color.FromArgb(255, 204, 170);
						}
					}
					catch (FormatException)
					{
					}
				}
			}
		}

		protected SortDirection SortDir
		{
			get
			{
				if (ViewState["dirState"] == null)
					ViewState["dirState"] = SortDirection.Ascending;

				return (SortDirection)ViewState["dirState"];
			}
			set
			{
				ViewState["dirState"] = value;
			}
		}

		protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
		{
			SortDir = SortDir == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;
			this.GridView1.DataSource = new VendorOffers(this.UserInfo).Sort(e.SortExpression, SortDir == SortDirection.Ascending);
			this.GridView1.DataBind();
		}
	}
}